package com.easybuy.controller;

import com.easybuy.Tools.Page;
import com.easybuy.pojo.User;
import com.easybuy.service.UserService.UserService;
import com.ndktools.javamd5.Mademd5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class Member_UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/userList")
    @ResponseBody
    public Page user(Page page){
        page=userService.userList(page);
        return page;
    }
    @RequestMapping("/getUser")
    @ResponseBody
    public User getUser(@RequestParam int id){
        User user=userService.getUser(id);
        String password=user.getPassword();
        return user;
    }
    @RequestMapping("/modifyUser")
    @ResponseBody
    public boolean Modify(User user){
        Mademd5 md5=new Mademd5();
        String password=null;
        if (user.getPassword()!=""){
            password=md5.toMd5(user.getPassword()).toLowerCase();
        }
        user.setPassword(password);
        if (userService.modifyUser(user)>0){
            return true;
        }
        else return false;
    }
    @RequestMapping("/deleUser")
    @ResponseBody
    public boolean deleUser(int id){
        if (userService.deleUser(id)>0){
            return true;
        }
        else return false;
    }
    @RequestMapping("/checkUser")
    @ResponseBody
    public User checkUser(@RequestParam String loginName){
        User user=userService.selectByLoginName(loginName);
        return user;
    }
}
